//
//  WmcEditBrandChangeLocationView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/13.
//

import UIKit
import YPBedrock

// MARK: - [WmcEditBrandChangeLocationView]

class WmcEditBrandChangeLocationView: WmcSelectView {
    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout
            .YP.CreateGridLayout(scrollDirection: .horizontal,
                                 minimumLineSpacing: 12,
                                 minimumInteritemSpacing: 0,
                                 sectionInset: UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12))
        return layout
    }()

    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView
            .YP.Create(layout: layout,
                       delegate: self,
                       dataSource: self,
                       registers: [
                           YPRegister(WmcEditBrandChangeLocationCell.self),
                       ])
        collectionView.bounces = false
        return collectionView
    }()

    var locationList: [WmcEditBrandChangeLocationCell.Model] = [
        .init(type: .followWatermark, isSelect: false),
        .init(type: .topLeft, isSelect: false),
        .init(type: .topRight, isSelect: false),
        .init(type: .center, isSelect: false),
        .init(type: .bottomLeft, isSelect: false),
        .init(type: .bottomRight, isSelect: false),
    ]

    /// 选择了一种样式
    var locationTypeChangeBlock: YPBlock.OneParam<WmcWatermarkOptionModel.Brand.LocationType>?

    override func makeUI() {
        super.makeUI()

        title = "换位置"
        titleView.leftBtn.setImage("close".wmc_image, for: .normal)

        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.equalTo(135)
        }
    }

    /// 设置选中的类型
    func setLocationType(_ type: WmcWatermarkOptionModel.Brand.LocationType) {
        locationList.forEach { model in
            model.isSelect = (model.type == type)
        }
    }
}

// MARK: - UICollectionViewDelegate

extension WmcEditBrandChangeLocationView: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        locationList.forEach { model in
            model.isSelect = false
        }
        if let model = locationList.yp.object(at: indexPath.row) {
            model.isSelect = !model.isSelect
            collectionView.reloadData()

            locationTypeChangeBlock?(model.type)
        }
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension WmcEditBrandChangeLocationView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lineCount: CGFloat = 4
        let lineSpacing = self.layout.minimumLineSpacing * (lineCount - 1)
        let horizontalEdge = self.layout.sectionInset.yp.horizontal
        let width = (collectionView.frame.size.width - lineSpacing - horizontalEdge) / lineCount - 1
        let height = collectionView.frame.height - self.layout.sectionInset.yp.vertical - self.layout.minimumInteritemSpacing
        return CGSize(width: width, height: height)
    }
}

// MARK: - UICollectionViewDataSource

extension WmcEditBrandChangeLocationView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return locationList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WmcEditBrandChangeLocationCell = collectionView.yp.dequeueReusableCell(for: indexPath)
        if let model = locationList.yp.object(at: indexPath.row) {
            cell.configModel(model)
        }
        return cell
    }
}
